/*
 * @Autor: zengy
 * @Date: 2021-07-20 09:31:19
 * @LastEditors: zengy
 * @LastEditTime: 2021-07-20 13:56:40
 * @Description: flexible多尺寸适配方案
 * @FilePath: \Mics3.0_3DViewer_WEB\3DViewer\src\plugins\flexible.js
 */

import store from '../store';

const { commit } = store;

(function flexible (window, document) {
  //基准比例为1
  let baseRatio = 81.5;
  let currentFontRatio = 1;
  Object.defineProperty(window,'__RATIO',{
    get(){return currentFontRatio },
    set(val){
      currentFontRatio = val/baseRatio;
      setRemUnit();
    }
  });
  // body font适配
  function setBodyFontSize () {
    if (document.body) {
      document.body.style.fontSize = (12 * window.devicePixelRatio || 1) + 'px'
    }
    else {
      document.addEventListener('DOMContentLoaded', setBodyFontSize)
    }
  }
  function setRemUnit () {
    let ele = document.documentElement;
    ele.style.fontSize = ele.clientWidth / 10 / currentFontRatio + 'px';
    commit('setPX', parseFloat(document.documentElement.style.fontSize));
  }
  //拖放适配方案
  window.addEventListener('resize', setRemUnit)
  window.addEventListener('pageshow', function (e) {
    if (e.persisted) {
      setRemUnit()
    }
  });
  setBodyFontSize();
  setRemUnit();

}(window, document))
